package com.android.fragmentdemo.adapters
import android.content.Context
import com.android.fragmentdemo.R
import com.hengfoxwolf.commonbase.base.databeans.SlipItem
import com.android.fragmentdemo.databinding.RvCardItemLayoutBinding
import com.android.fragmentdemo.definervlayoutmanager.ICardCallback
import com.hengfoxwolf.commonbase.base.adapter.BaseRvAdapter

class CardItemAdapter(private val context: Context) : BaseRvAdapter<SlipItem, RvCardItemLayoutBinding>(), ICardCallback {

    private var itemNumber = 0

    override fun getLayoutId(): Int = R.layout.rv_card_item_layout

    override fun bindData(holder: ViewHolder, dataItem: SlipItem, position: Int) {
        holder.binding.rvCardRoot.setCardBackgroundColor(context.resources.getColor(dataItem.color))
        val index = if (position + 1 + itemNumber == 0) itemDataSet?.size ?: 0 else position + 1 + itemNumber
        val textNumber = "$index / ${(itemDataSet?.size ?: 0)}"
        holder.binding.tvCardIndex.text = textNumber
    }

    override fun onItemRemove(position: Int) {
        itemDataSet?.let {
            val slipItem = it.removeAt(position)
            it.add(0, slipItem)
            itemNumber--
            itemNumber %= it.size
            notifyDataSetChanged()
        }
    }
}